package com.wgts.wgts.mvp.view.activity.search;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.control.labels.LabelData;
import com.wgts.wgts.control.labels.LabelsView;
import com.wgts.wgts.control.recycleview.CustomLineaLayoutManager;
import com.wgts.wgts.control.recycleview.CustomerGridLayoutManager;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerAppComponent;
import com.wgts.wgts.dagger.component.DaggerSearchActivityComponent;
import com.wgts.wgts.dagger.module.SearchActivityModule;
import com.wgts.wgts.mvp.presenter.ISearchActivityPersenter;
import com.wgts.wgts.mvp.view.ISearchActivityView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 搜索界面
 */

public class SearchActivity extends BaseActivity implements ISearchActivityView{

    public static final String EDITTEXT = "edittext";

    @BindView(R.id.cancle)
    LinearLayout cancle;
    @BindView(R.id.recycle_hot_search)
    RecyclerView recycleHotSearch;
    @BindView(R.id.recycle_history)
    RecyclerView recycleHistory;
    @BindView(R.id.img_clear)
    ImageView imgClear;
    @BindView(R.id.edit_search)
    EditText editSearch;
    @BindView(R.id.recycle_search)
    RecyclerView recycleSearch;
    @BindView(R.id.linear_hot)
    LinearLayout linearHot;
    @BindView(R.id.linear_history)
    LinearLayout linearHistory;
    @BindView(R.id.img_search_empty)
    ImageView imgSearchEmpty;
    @BindView(R.id.img_search)
    ImageView imgSearch;
    @BindView(R.id.hot_search_labels)
    LabelsView hotSearchLabels;
    @BindView(R.id.history_labels)
    LabelsView historyLabels;
    @BindView(R.id.tv_cancle)
    TextView tvCancle;

    private List<LabelData> labelDatas;

    private CustomerGridLayoutManager layoutManager;

    private CustomLineaLayoutManager lineaLayoutManager;


    private ArrayList<String> srcDate;

    private String mKeyWord = "";

    private SearchResultAdatper searchResultAdatper;

    @Inject
    ISearchActivityPersenter persenter;

    @Override
    public void init() {
        DaggerSearchActivityComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .searchActivityModule(new SearchActivityModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void onCreate(Bundle saveInstanceBundle) {
        super.onCreate(saveInstanceBundle);
        setContentView(R.layout.search_activity);
        ButterKnife.bind(this);
        initView();
    }


    private void initView() {
        persenter.HotSearchResult();
        persenter.getHistorySearch();
        LabelsClick();

        recycleSearch.setVisibility(View.GONE);
        layoutManager = new CustomerGridLayoutManager(this, 5);
        recycleHotSearch.setLayoutManager(layoutManager);
        layoutManager = new CustomerGridLayoutManager(this, 5);
        recycleHistory.setLayoutManager(layoutManager);

        recycleHotSearch.setAdapter(new SearchHistoryHotAdapter(srcDate, this));
        recycleHistory.setAdapter(new SearchHistoryHotAdapter(srcDate, this));

        lineaLayoutManager = new CustomLineaLayoutManager(this);
        lineaLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleSearch.setLayoutManager(lineaLayoutManager);


        imgClear.setVisibility(View.INVISIBLE);



        editSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    String input = editSearch.getText().toString();
                    if (!TextUtils.isEmpty(input)) {
                        persenter.WriteHistorySearch(input);
                        Intent intent = new Intent(SearchActivity.this, SearchResultActivity.class);
                        intent.putExtra(EDITTEXT, input);
                        startActivity(intent);
                    }
                    return true;
                }
                return false;
            }
        });

        editSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(s != null && !s.toString().equals("")){
                    tvCancle.setText("搜索");
                }else{
                    tvCancle.setText("取消");
                }
            }
        });

    }

    @OnClick({R.id.img_clear, R.id.img_search_empty, R.id.tv_cancle})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_clear:
                editSearch.setText("");
                break;
            case R.id.img_search_empty:
                persenter.clearHistorySearch();
                break;
            case R.id.tv_cancle:
                if(editSearch.getText().toString().length() > 0){
                    persenter.WriteHistorySearch(editSearch.getText().toString());
                    Intent intent = new Intent(SearchActivity.this, SearchResultActivity.class);
                    intent.putExtra(EDITTEXT, editSearch.getText().toString());
                    startActivityForResult(intent, 0);
                }else{
                   finish();
                }
                break;
        }
    }

    private void LabelsClick() {
        //标签点击事件，无样式更改
        hotSearchLabels.setOnLabelClickListener(new LabelsView.OnLabelClickListener() {
            @Override
            public void onLabelClick(View label, LabelData data, int position) {
                editSearch.setText(data.getName());
                editSearch.setSelection(data.getName().length());
                persenter.WriteHistorySearch(data.getName());
                Intent intent = new Intent(SearchActivity.this, SearchResultActivity.class);
                intent.putExtra(EDITTEXT, data.getName());
                startActivityForResult(intent, 0);
            }
        });
        historyLabels.setOnLabelClickListener(new LabelsView.OnLabelClickListener() {
            @Override
            public void onLabelClick(View label, LabelData data, int position) {
                editSearch.setText(data.getName());
                editSearch.setSelection(data.getName().length());
                Intent intent = new Intent(SearchActivity.this, SearchResultActivity.class);
                intent.putExtra(EDITTEXT, data.getName());
                startActivityForResult(intent, 0);
            }
        });

        //标签选中事件，有样式改变
        hotSearchLabels.setOnLabelSelectChangeListener(new LabelsView.OnLabelSelectChangeListener() {
            @Override
            public void onLabelSelectChange(View label, LabelData data, boolean isSelect, int position) {
                TextView textView = (TextView) label;
                if (isSelect) {
                    ((TextView) label).setTextColor(ContextCompat.getColor(SearchActivity.this, R.color.whrite));
                } else {
                    ((TextView) label).setTextColor(ContextCompat.getColor(SearchActivity.this, R.color.hint_6));
                }
                data.getName();
                data.getId();
            }
        });
    }

    @Override
    public void hotSearchResult(List<LabelData> list) {
        hotSearchLabels.setLabels(list);
    }

    @Override
    public void getHistorySearch(List<LabelData> list) {
        if(list.size() > 0){
            linearHistory.setVisibility(View.VISIBLE);
        }else{
            linearHistory.setVisibility(View.GONE);
        }
        historyLabels.setLabels(list);
    }

    @Override
    public void onError(int status, String msg) {

    }
    //搜索点击事件
    private void searchPoint(){

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data){
        super.onActivityResult(requestCode, resultCode, data);
        persenter.getHistorySearch();
    }
}
